package com.taillive.commons.io;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class ImageConvert {

	private static Logger logger = LogManager.getLogger(ImageConvert.class);

	private String convertCmd = "C:\\Program Files\\ImageMagick-6.4.0-Q16\\convert.exe";

	// following vars will be init in construction method
	private Image image;

	private String srcImageFile;

	private float width;

	private float height;

	public String getConvertCmd() {
		return convertCmd;
	}

	public void setConvertCmd(String convertCmd) {
		this.convertCmd = convertCmd;
	}

	public ImageConvert(String srcImageFile) {
		loadImage(srcImageFile);
	}

	public ImageConvert(File srcImageFile) {

		loadImage(srcImageFile.getAbsolutePath());

	}

	private void loadImage(String imageFile) {

		srcImageFile = imageFile;

		Toolkit toolkit = Toolkit.getDefaultToolkit();
		image = toolkit.getImage(imageFile);

		Container c = new Container();
		MediaTracker mediaTracker = new MediaTracker(c);
		mediaTracker.addImage(image, 0);

		try {
			mediaTracker.waitForID(0);
		} catch (InterruptedException e) {
			logger.error("Load image", e);
		}

		width = image.getWidth(null);
		height = image.getHeight(null);

	}

	private Dimension getOutputDimension(float scale) {
		if (scale > 1) {
			scale = 1;
		}
		return new Dimension((int) (width * scale), (int) (height * scale));
	}

	private Dimension getOutputDimensionByWidth(int maxSize) {
		return getOutputDimension(maxSize / width);
	}

	private Dimension getOutputDimensionByHeight(int maxSize) {
		return getOutputDimension(maxSize / height);

	}

	private Dimension getOutputDimension(int maxSize) {
		if (width >= height) {
			return getOutputDimensionByWidth(maxSize);
		} else {
			return getOutputDimensionByHeight(maxSize);
		}
	}
	
	

	public void resize(String destImageFile, int maxSize) {
		Dimension dimension = getOutputDimension(maxSize);
		resize(srcImageFile, destImageFile, (int) dimension.getWidth(),
				(int) dimension.getHeight());
	}
	


	public void resizeByWidth(String destImageFile, int maxSize) {
		Dimension dimension = getOutputDimensionByWidth(maxSize);
		resize(srcImageFile, destImageFile, (int) dimension.getWidth(),
				(int) dimension.getHeight());
	}

	public void resizeByHeight(String destImageFile, int maxSize) {
		Dimension dimension = getOutputDimensionByHeight(maxSize);
		resize(srcImageFile, destImageFile, (int) dimension.getWidth(),
				(int) dimension.getHeight());
	}

	/**
	 * sourceImageFile: Source Image file that will be resized destImageFile:
	 * Destination Image file that will be output maxWidth: max width the output
	 * image file will be
	 */
	public void resize(String srcImageFile, String destImageFile, int width,
			int height) {

		// String command = convertCmd + " -scale "
		// + 100 + "x" + 100 + " "
		// + srcImageFile + " " + destImageFile;

		String command = convertCmd
				+ " "
				+ srcImageFile
				+ " -resize "+width+"x"+height+"  +profile \"*\" "
				+ " -quality 80 "
				+ destImageFile;
		
		exec(command);

	}

	
	public void resizeToSquare(String destImageFile, int maxSize) {
		int side = 0;
		int x = 0;
		int y = 0;
		
		
		if(width==height){
			side = (int)width;
		}else if(width>height){
			side = (int)height;
			x = ((int)width - (int)height)/2;
		}else{
			side = (int)width;
			y = ((int)height - (int)width)/2;
		}
		resizeToSquare(srcImageFile, destImageFile, side, maxSize, x, y);
	}
	
	public void resizeToSquare(String srcImageFile, String destImageFile,int originSide,int maxSize,int x,int y) {
		

		
		String command = convertCmd
		+ " " + srcImageFile + 
		" -crop "+originSide+"x"+originSide+"+"+x+"+"+y +
		" -resize "+maxSize+"x+"+maxSize+" +profile \"*\" "
		+ " -quality 80 "
		+ destImageFile;

		exec(command);
	}

	public void exec(String command) {
		logger.info(command);
		try {
			Process process = Runtime.getRuntime().exec(command);
			process.waitFor();
		} catch (Exception e) {
			logger.error("Image resize error", e);
		}
	}
}
